package com.cysoft.starter.repeat.core.helper;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 玖零
 * @version V0.0.1
 * @project cysoft-spring-boot-starter-repeat
 * @package com.cysoft.starter.repeat.core.helper
 * @date 2025-03-02 23:01
 * @desc
 */
public class RequestParamsHelper {

    // 定义一个私有的构造函数，防止类被实例化
    private RequestParamsHelper() {
        // 可以抛出异常，防止在类内部不小心调用构造函数
        throw new AssertionError("This class should not be instantiated.");
    }

    private static final String HTTP_SERVLET_REQUEST = "HttpServletRequest";
    private static final String HTTP_SERVLET_RESPONSE = "HttpServletResponse";
    private static final String MULTIPART_FILE = "MultipartFile";

    public static String getRequestParams(ProceedingJoinPoint point) {
        try {
            // 获取方法的参数名
            String[] parameterNames = getParameterNames(point);
            // 获取方法的参数值
            Object[] args = point.getArgs();
            // 检查参数名或参数值是否为空或不匹配
            if (shouldReturnEmptyMap(parameterNames, args)) {
                return "";
            }
            // 构建参数拼接结果
            return buildParameterString(parameterNames, args);
        } catch (Exception e) {
            // 处理异常，返回空字符串
            return "";
        }
    }

    /**
     * 获取方法的参数名
     *
     * @param point 切入点
     * @return 方法的参数名数组
     */
    private static String[] getParameterNames(ProceedingJoinPoint point) {
        return ((MethodSignature) point.getSignature()).getParameterNames();
    }

    /**
     * 检查是否应该返回空字符串
     *
     * @param parameterNames 方法的参数名数组
     * @param args           方法的参数值数组
     * @return 如果应该返回空字符串，则返回true；否则返回false
     */
    private static boolean shouldReturnEmptyMap(String[] parameterNames, Object[] args) {
        return parameterNames == null || parameterNames.length > args.length;
    }

    /**
     * 构建参数拼接字符串
     *
     * @param parameterNames 方法的参数名数组
     * @param args           方法的参数值数组
     * @return 参数拼接字符串
     */
    private static String buildParameterString(String[] parameterNames, Object[] args) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < parameterNames.length; i++) {
            if (i > 0) {
                sb.append("&");
            }
            String paramName = parameterNames[i];
            Object object = args[i];
            sb.append(paramName).append("=").append(getParameterValue(object));
        }
        return sb.toString();
    }

    /**
     * 根据参数类型获取参数值
     *
     * @param object 参数对象
     * @return 参数值
     */
    private static Object getParameterValue(Object object) {
        if (object instanceof HttpServletRequest) {
            return HTTP_SERVLET_REQUEST;
        } else if (object instanceof HttpServletResponse) {
            return HTTP_SERVLET_RESPONSE;
        } else if (object instanceof MultipartFile) {
            return MULTIPART_FILE;
        }
        return object != null ? object.toString() : "null";
    }


}
